#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 rambla.eu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.import os
import json, os
import raws_json
from raws_json.raws_service import RawsService

class RassService(RawsService):

    def __init__(self, username=None, password=None, server=None, ssl = False):
        self.username = username
        super(RassService, self).__init__(username = username, password = password, server = server, ssl = ssl)

    def delete(self, uri):
        """ Deletes any resource, given the uri. """
        return self.Delete(uri = uri)

    def getItemHeader(self, uri):
        return self.Head(uri = uri)

    def getItemHeaderFromPath(self, path):
        return self.getItemHeader(uri = "/item/" + path.lstrip("/"))

    def postItem(self, dirpath, filename, local_path):
        """ Tries to POST a new item resource to RASS. 
            Note that POST URI path shouldn't contain the filename. Therefore, the first param is named dirpath.
            Note also that if the file already exists, RASS will add a numerical suffix to the filename and create it anyway.
            Therefore, you should check the response (return value) for the exact filename.
    
            @param path : path to the directory (on the rambla CDN) in which the item needs to be created.
            @param filename proposed filename to be used when storing the file (RASS may append a suffix if file already exists on CDN).
            @param local_path location of the file to be uploaded on the local machine
    
            @return Item object
        """
        uri = "/item/" + dirpath.lstrip("/")
        media_source = raws_json.MediaSource(file_path = local_path, svr_filename = filename)
        media_entry = self.Post(data = None, uri = uri, media_source = media_source)
        return media_entry

    def putItem(self, path, local_path):
        """ Tries to PUT a new item resource to RASS.
            Note that PUT URI path should contain the filename.
            If the file already exists, PUT will fail.

            @param path : path to be given to the uploaded file on the RASS server.
            @param local_path location of the file to be uploaded on the local machine

            @return Item object
        """
        uri = "/item/" + path.lstrip("/")
        media_source = raws_json.MediaSource(file_path = local_path, svr_filename = None)
        media_entry = self.Put(data = None, uri = uri, media_source = media_source)
        return media_entry
        
    def createDir(self, path, force_create = False):
        uri = "/dir/" + path.lstrip("/")
        if force_create:
            return self.Post(data = None, uri = uri)
        else:
            return self.Put(data = None, uri = uri)
        
    def getDirList(self, path, query = None):
        """ Retrieves a vocab list. 

            @param path : path to the directory to be retrieved
            @param query raws_json.Query object that contains queryset args.
            @return Dir feed.
        """
        uri = "/dir/" + path.lstrip("/")
        if query:
            query.feed = uri
            uri = query.ToUri()
        return self.Get(uri = uri)


    # def getItemFile(self, uri, local_path = None):
    #     """ Downloads a item file and (optionally) saves it to a local file location. 
    # 
    #         If local_path is given, an attempt is made to save the downloaded file and an exception is thrown in case of failure.
    #         If local_path is None, the downloaded file is not stored. The caller should store it himself using the file_handle attribute of the returned MediaSource object.
    # 
    #         @param uri path (relative or absolute) to the item media item.
    #         @param local_path full path, including the filename, to a location where the downloaded file can be stored.
    #         @return rawsc.MediaSource object
    #     """
    #     if uri is None:
    #       raise rawsc.RawscException('You must provide a valid URI argument to getItemFile().')
    #     return self.GetMedia(uri = uri, file_path = local_path)
    # 
    # def getItemFromPath(self, path, local_path = None):
    #     """ Downloads a item file and (optionally) saves it to a local file location. 
    # 
    #         If local_path is given, an attempt is made to save the downloaded file and an exception is thrown in case of failure.
    #         If local_path is None, the downloaded file is not stored. The caller should store it himself using the file_handle attribute of the returned MediaSource object.
    # 
    #         @param path : relative path to the dir instance, starting from the root.
    #         @param local_path full path, including the filename, to a location where the downloaded file can be stored.
    #         @return rawsc.MediaSource object
    #     """
    #     return self.getItemFile(uri = "/item/" + path.lstrip("/"), local_path = local_path)

